package com.wang.controller;

import com.google.gson.Gson;
import com.wang.entry.Book;
import com.wang.pojo.BookDelete;
import com.wang.service.BookService;
import com.wang.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/book/*")
public class BookServlet extends HttpServlet {

    BookService bookService = new BookServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset-8");

        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = BookServlet.class;

        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }


    }

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<Book> books = bookService.selectAll();
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(books));


    }


    public void queryByBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String query = request.getParameter("query");
        String str = request.getParameter("str");

        Book book = new Book();
        if(query.equals("1")) {
            book.setBookName(str);
        }else if(query.equals("2")) {
            book.setBookAuthor(str);
        }else if(query.equals("3")) {
            book.setBookPublish(str);
        }else {

        }
        ArrayList<Book> books = bookService.selectQuery(book);
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(books));

    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookId = request.getParameter("bookId");
        BookDelete bookDelete = new BookDelete(bookService.deleteBookById(Integer.parseInt(bookId)),bookService.selectAll());
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(bookDelete));
    }

    public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookId = request.getParameter("bookId");
        Book book = bookService.selectById(Integer.parseInt(bookId));
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(book));

    }

    public void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookId = request.getParameter("bookId");
        String bookName = request.getParameter("bookName");
        String bookAuthor = request.getParameter("bookAuthor");
        String bookPublish = request.getParameter("bookPublish");
        String bookPage = request.getParameter("bookPage");
        String bookPrice = request.getParameter("bookPrice");
        String createDate = request.getParameter("createDate");

        boolean boo = bookService.updateBook(new Book(Integer.parseInt(bookId), bookName,
                bookAuthor, bookPublish, Integer.parseInt(bookPage), Double.parseDouble(bookPrice), createDate));

        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(boo));
    }

    public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookName = request.getParameter("bookName");
        String bookAuthor = request.getParameter("bookAuthor");
        String bookPublish = request.getParameter("bookPublish");
        String bookPage = request.getParameter("bookPage");
        String bookPrice = request.getParameter("bookPrice");
        String createDate = request.getParameter("createDate");
        Book book = new Book(bookName, bookAuthor, bookPublish,
                Integer.parseInt(bookPage), Double.parseDouble(bookPrice), createDate);

        boolean boo = bookService.insertBook(book);

        System.out.println(book);

        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(boo));
    }

    }
